package com.thinking.project.monitorserver.config;


import javax.servlet.http.HttpServletRequest;

import com.thinking.project.monitorserver.util.ResponseData;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;


/***
 * @Author tangsixiang@163.com
 * @Description //TODO    应用层全局异常处理拦截器
 * @Date   14:49 2019/11/25
 **/
@ControllerAdvice
public class GlobalDefaultExceptionHandler {

    /***
     * @Author tangsixiang@163.com 系统异常
     **/
    @ExceptionHandler(Exception.class)
    public ResponseData defaultExceptionHandler(HttpServletRequest req, Exception e) {
        e.printStackTrace();
        return ResponseData.serverInternalError().putDataValue("mes", e.getMessage());
    }

    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public ResponseData notFount(RuntimeException e) {
        return ResponseData.serverInternalError().putDataValue("mes","运行时异常:" + e.getMessage() );
    }

    /**
     * 请求方式不支持
     */
    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
    public ResponseData handleException(HttpRequestMethodNotSupportedException e) {

        return ResponseData.serverInternalError().putDataValue("mes","不支持' " + e.getMethod() + "'请求");
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(BindException.class)
    public ResponseData validatedBindException(BindException e) {
        String message =e.getAllErrors().get(0).getDefaultMessage();
        return ResponseData.serverInternalError().putDataValue("mes",message );
    }




}
